﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernate;
using NHibernate.Cfg;
using System.Reflection;
using DemoManyTowOne.Domain;
namespace DemoManyTowOne.IService
{
    class Nhibernate
    {
        private ISession mySession;
        static ISessionFactory mySecsionFactory;
        static ISession OpenSession()
        {
            if (mySecsionFactory == null)
            {
                var configuration = new Configuration();
                configuration.AddAssembly(Assembly.GetCallingAssembly());
                mySecsionFactory = configuration.BuildSessionFactory();
            }
            return mySecsionFactory.OpenSession();
        }
        public IList<tblCon> LayThongTinCon()
        {
            using (mySession = OpenSession())
            {
                var criteria = mySession.CreateCriteria<tblCon>();
                var list = criteria.List<tblCon>();
                return list;
            }
        }
        public void Them(tblCha cha)
        {
            using (mySession = OpenSession())
            {
                mySession.Save(cha);
                mySession.Flush();
            }
        }
        public void Sua(tblCha cha)
        {
            using (mySession = OpenSession())
            {
                mySession.Update(cha);
                mySession.Flush();
            }
        }

        public void ThemCon(tblCon con)
        {
            try
            {
                using (mySession = OpenSession())
                {
                    //mySession.CreateQuery("update tblCon set FirtNameCon =:a where ID = :b")
                    //    .SetParameter("b",2)
                    //    .SetParameter("a", "abc")
                    //    .ExecuteUpdate();
                    mySession.Save(con);
                    mySession.Flush();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
